// 给你一个 m 行 n 列的矩阵 matrix ，请按照 顺时针螺旋顺序 ，返回矩阵中的所有元素。
/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
var spiralOrder = function(matrix) {
    if (matrix.length === 0) return [];
    let up = 0, down = matrix.length - 1, left = 0, right = matrix[0].length - 1;
    let result = [];
    while(true) {
        for(let i = left; i <= right; i++) {
            result.push(matrix[up][i]);
        }
        ++up;
        if (up > down) break;

        for(let i = up; i <= down; i++) {
            result.push(matrix[i][right]);
        }
        right--;
        if (right < left) break;

        for(let i = right; i >= left; i--) {
            result.push(matrix[down][i]);
        }
        down--;
        if (down < up) break;

        for(let i = down; i >= up; i--) {
            result.push(matrix[i][left]);
        }
        left++;
        if (left > right) break;
    }
    return result;
};

console.log(spiralOrder([[1,2,3],[4,5,6],[7,8,9]]));




